﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author(s): Ryan Redburn
    // Additional Authors:
    // Date: 9/30/2013

    #region [ Updates ]

    // Updated: 10/5/2013
    // By: Ryan Redburn
    // Updates: Changed the Log In form to be the parent form,
    // transferred the appropriate material from the GeneralOverview form to this form

    #endregion

    public partial class FormLogIn : Form
    {
        private bool createDefault;

        #region [ Constructors ]

        public FormLogIn(bool arg)
        {
            InitializeComponent();
            createDefault = arg;
        }

        #endregion

        #region [ Events ]

        // Function has now changed, fix this
        private void FormLogIn_Load(object sender, EventArgs e)
        {
            if (createDefault)
            {
                FormCreate FormCreate = new FormCreate(createDefault);
                FormCreate.ShowDialog();
            }
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            try
            {
                if (AccountKeeper.ValidateUser(txtUserID.Text, txtPassword.Text) == true)
                {
                    CurrentUser.Intialization(txtUserID.Text);
                    this.Hide();
                    FormGeneralOverview FormGeneralOverview = new FormGeneralOverview();
                    FormGeneralOverview.ShowDialog();
                    LogOut();
                }
                else
                {
                    MessageBox.Show("You have entered either an invalid user ID or password.",
                        "Invalid User ID or Password");
                    txtPassword.Focus();
                    txtPassword.SelectAll();
                }
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("The given user account does not exist.");
                txtUserID.Clear();
                txtPassword.Clear();
                txtUserID.Focus();
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            FormCreate FormCreate = new FormCreate();
            FormCreate.ShowDialog();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Confirm Exit",
           MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        #endregion

        #region [ Methods ]

        private void LogOut()
        {
            this.Show();
            txtUserID.Clear();
            txtPassword.Clear();
            txtUserID.Focus();
        }

        #endregion
    }
}
